<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 删除按钮 -->
			<!-- <cl-multi-delete-btn /> -->
			<cl-add-btn />
			<!-- 导出按钮 -->
			<cl-export-btn :columns="Table?.columns" />
			<cl-flex1 />

			<!-- 搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table">
				<template #slot-appoint-btn="{ scope }">
					<el-button
						type="primary"
						@click="
							() => {
								showAppointDialog(scope.row.id);
							}
						"
						>预约
					</el-button>
				</template>
			</cl-table>
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
	<!--预约弹窗-->
	<week-appoint-dialog
		v-model="appointDialogVisible"
		:student-id="currentStudentId"
		@success="refresh"
	/>
</template>

<script lang="ts" setup>
defineOptions({
	name: 'user-list'
});

import { useCrud, useSearch, useTable, useUpsert } from '@cool-vue/crud';
import { useI18n } from 'vue-i18n';
import { useCool } from '/@/cool';
import { reactive, ref } from 'vue';
import { useDict } from '/$/dict';
import { useBase } from '/$/base';
import WeekAppointDialog from '../../appointment/components/WeekAppointDialog.vue';
import QrcodeSelect from '/$/qrcode/components/qrcode-select.vue';

const { t } = useI18n();
const { service } = useCool();
const { dict } = useDict();
const { user } = useBase();

const appointDialogVisible = ref(false);
const currentStudentId = ref(0);

const options = reactive({
	loginType: [
		{
			label: t('小程序'),
			value: 0,
			type: 'danger'
		},
		{
			label: t('公众号'),
			value: 1,
			type: 'success'
		},
		{
			label: t('H5'),
			value: 2
		}
	],
	gender: [
		{
			label: t('未知'),
			value: 0,
			type: 'info'
		},
		{
			label: t('男'),
			value: 1,
			type: 'success'
		},
		{
			label: t('女'),
			value: 2,
			type: 'danger'
		}
	],
	status: [
		{
			label: t('禁用'),
			value: 0,
			type: 'danger'
		},
		{
			label: t('正常'),
			value: 1,
			type: 'success'
		},
		{
			label: t('已注销'),
			value: 2,
			type: 'warning'
		}
	],
	shopList: [],
	addWechat: [
		{
			label: t('已添加'),
			value: 1,
			type: 'success'
		},
		{
			label: t('未添加'),
			value: 0,
			type: 'danger'
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		// {
		// 	type: 'selection',
		// 	width: 60
		// },
		{
			label: t('ID'),
			prop: 'id',
			minWidth: 80
		},
		{
			label: t('门店'),
			prop: 'lastAppointShopName',
			minWidth: 120
		},
		{
			label: t('预约时间'),
			prop: 'lastAppointTime',
			minWidth: 200
		},
		{
			label: t('添加微信'),
			prop: 'addWechat',
			component: {
				name: 'cl-switch'
			}
		},
		{
			label: t('姓名'),
			prop: 'nickName',
			minWidth: 150
		},
		// {
		// 	label: t('头像'),
		// 	prop: 'avatarUrl',
		// 	minWidth: 100,
		// 	component: {
		// 		name: 'cl-avatar'
		// 	}
		// },
		{
			label: t('手机号'),
			prop: 'phone',
			minWidth: 120
		},
		{
			label: t('销售'),
			prop: 'adminName',
			minWidth: 120
		},
		// 达人
		{
			label: t('达人'),
			prop: 'darenName',
			minWidth: 120
		},
		{
			label: t('渠道'),
			prop: 'registChannelId',
			dict: dict.get('Channel'),
			minWidth: 120
		},
		{
			label: t('性别'),
			prop: 'gender',
			dict: options.gender,
			minWidth: 120
		},

		// {
		// 	label: t('登录方式'),
		// 	prop: 'loginType',
		// 	dict: options.loginType,
		// 	minWidth: 120
		// },
		// {
		// 	label: t('状态'),
		// 	prop: 'status',
		// 	minWidth: 120,
		// 	dict: options.status
		// },
		{ label: t('备注'), prop: 'remark', showOverflowTooltip: true },
		{
			label: t('创建时间'),
			prop: 'createTime',
			// sortable: 'desc',
			minWidth: 170
		},
		{
			type: 'op',
			buttons: ['slot-appoint-btn', 'edit'],
			width: 200
		}
	]
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		// 姓名
		{
			prop: 'nickName',
			label: '姓名',
			component: { name: 'el-input' },
			span: 24
		},
		// 手机号
		{
			prop: 'phone',
			label: '手机号',
			component: { name: 'el-input' },
			span: 12
			// hidden: ({ scope }) => scope.phone
		},
		{
			label: t('选择二维码'),
			prop: 'qrcodeId',
			component: {
				vm: QrcodeSelect,
				props: {
					modelValue: '' // 初始值
				}
			},
			span: 12
			// hidden: ({ scope }) => scope.phone
		},
		// 选择生日
		{
			prop: 'birthday',
			label: '选择生日',
			component: { name: 'el-date-picker' },
			span: 12
		},
		// 选择性别
		{
			prop: 'gender',
			label: '选择性别',
			component: { name: 'el-radio-group', options: options.gender },
			span: 12
		},
		{
			label: t('备注'),
			prop: 'remark',
			component: {
				name: 'el-input',
				props: { type: 'textarea', rows: 4 }
			}
		}
	],
	onOpen() {
		service.order.shop.getSelectList().then((res: any) => {
			if (res) {
				if (res) {
					const shopItem = Upsert.value.items.find(item => item.prop === 'shopId');
					shopItem.component.options = res;
				}
			}
		});
	}
});

// cl-search
const Search = useSearch();

// cl-crud
const Crud = useCrud(
	{
		service: service.user.info,
		async onRefresh(params, { next, done, render }) {
			params.registAdminId = user.info?.id;
			Crud.value.params = params;
			// params.isAgent = true;
			Crud.value.params.payStatus = params.payStatus;
			Crud.value.params.orderProcess = params.orderProcess;
			Crud.value.params.isAgent = params.isAgent;
			const { list } = await next(params);
			// 渲染数据
			render(list);
		}
	},
	app => {
		app.refresh();
	}
);

// 添加editUser方法
const editUser = async userId => {
	// 打开编辑弹窗并回显数据
	Upsert.value.edit({ id: userId });
};

function showAppointDialog(studentId) {
	currentStudentId.value = studentId;
	appointDialogVisible.value = true;
	// 刷新表单
	Crud.value?.refresh();
}

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
